package org.az.netty_study.p14_netty综合案例2_聊天室.server.handler;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.az.netty_study.p14_netty综合案例2_聊天室.message.ChatRequestMessage;
import org.az.netty_study.p14_netty综合案例2_聊天室.message.ChatResponseMessage;
import org.az.netty_study.p14_netty综合案例2_聊天室.message.GroupChatRequestMessage;
import org.az.netty_study.p14_netty综合案例2_聊天室.message.GroupChatResponseMessage;
import org.az.netty_study.p14_netty综合案例2_聊天室.server.session.GroupSessionFactory;
import org.az.netty_study.p14_netty综合案例2_聊天室.server.session.SessionFactory;

import java.util.Set;

@ChannelHandler.Sharable
public class GroupChatRequestMessageHandler extends SimpleChannelInboundHandler<GroupChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupChatRequestMessage msg) throws Exception {
        Set<String> members = GroupSessionFactory.getGroupSession().getMembers(msg.getGroupName());
        for (String member : members) {
            Channel channel = SessionFactory.getSession().getChannel(member);
            channel.writeAndFlush(new GroupChatResponseMessage(msg.getFrom(), msg.getContent()));
        }
    }
}
